<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/2/16
 * Time: 下午3:36
 */

$str = "the spring festival!";
//以所有的每个字节值作为键名，出现次数作为值的数组。
$result = count_chars( $str );
print_r( $result );// 0--255 --> count

//只列出出现次数大于零的字节值。
$result = count_chars( $str, 1 );
//[32] => 2;[33] => 1;[97] => 1;[101] => 2;[102] => 1;[103] => 1;[104] => 1;[105] => 2;
//[108] => 1;[110] => 1;[112] => 1;[114] => 1;[115] => 2;[116] => 2;[118] => 1;
print_r( $result );

//但只列出出现次数等于零的字节值。
$result = count_chars( $str, 2 );
//返回由所有使用了的字节值组成的字符串。
// !aefghilnprstv
$result = count_chars( $str, 3 );
print_r( $result );

//返回由所有未使用的字节值组成的字符串。
$result = count_chars( $str, 4 );


function mb_count_chars( $input ) {
	$l      = mb_strlen( $input, 'UTF-8' );
	$unique = array();
	for ( $i = 0; $i < $l; $i ++ ) {
		$char = mb_substr( $input, $i, 1, 'UTF-8' );
		if ( ! array_key_exists( $char, $unique ) ) {
			$unique[ $char ] = 0;
		}
		$unique[ $char ] ++;
	}
	
	return $unique;
}

$str = "大年初一。";
//[大] => 1;[年] => 1;[初] => 1;[一] => 1;[。] => 1
print_r( mb_count_chars( $str ) );

function unichar( $string ) {
	$two = strtolower( str_replace( ' ', '', $string ) );
	$res = count( count_chars( $two, 1 ) );
	
	return $res;
}

echo unichar( "zing" ) . PHP_EOL; // 2
echo unichar( "bob" ) . PHP_EOL; // 2
echo unichar( "Invisibility" ) . PHP_EOL; //8
echo unichar( "The quick brown fox slyly jumped over the lazy dog" ) . PHP_EOL; //26